<?php
/**
 * Created by PhpStorm.
 * User: 35083
 * Date: 2019/7/11
 * Time: 14:26
 */

namespace app\queue;

class Queue
{
    static private $redisHander = null;

    const UserDefinedQueues = "UserDefinedQueues";//用户级别 就使用一个队列，就是本队列
    const UserTaskQueues = "UserTaskQueues";//系统任务队列
    const SleepReport = "SleepReport";//用户获取睡眠报告接口
    const SleepReportPre = "SleepReportPre";//用户获取睡眠报告接口
    const SleepReportPre10 = "SleepReportPre10";//用户获取睡眠报告接口
    const Addusermoneytask = "Addusermoneytask";//用户变动队列

    /**
     * 链接redis服务
     */
    static public function connect()
    {
        $config = config("cache.redis");
        self::$redisHander = new \Redis();
        self::$redisHander->connect($config['host'], $config['port'], $config['timeout']);
        self::$redisHander->auth($config['password']);
        self::$redisHander->select($config['select']);
    }

    /**
     * 向队列里面添加数据
     * @param $key
     * @param $val
     * @return 返回队列长度
     */
    static public function push($key, $val)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->lpush($key, $val);
    }


    /**
     * 从队列里面取数据
     * @param $key
     * @return 返回队列中的一个值
     */
    static public function pop($key)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->rpop($key);
    }

    /**
     * 用户级别队列
     * @param $key
     * @param $val
     * @return 返回队列长度
     */
    static public function userPush($val)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->lpush(self::UserDefinedQueues, $val);
    }

    /**
     * 从队列里面取数据
     * @param $key
     * @return 返回队列中的一个值
     */
    static public function userPop()
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->rpop(self::UserDefinedQueues);
    }


    /**
     * 用户级别队列
     * @param $key
     * @param $val
     * @return 返回队列长度
     */
    static public function systemTaskPush($val)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->lpush(self::UserTaskQueues, $val);
    }

    /**
     * 从队列里面取数据
     * @param $key
     * @return 返回队列中的一个值
     */
    static public function systemTaskPop()
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->rpop(self::UserTaskQueues);
    }

    /**
     * 获取队列长度
     * @param $key
     * @return int 返回队列长度
     */
    static public function length($key = self::UserDefinedQueues)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->lLen(self::UserDefinedQueues);
    }

    /**
     * redis订阅与发布，本函数是发布消息
     * @param $key
     * @param $msg
     * @return mixed
     */
    static public function publish($key, $msg)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->publish($key, $msg);
    }


    /**
     * 设置一个值 key=>val
     * @param $key
     * @param $val
     * @param $expare 过期时间 秒
     * @return mixed
     */
    static public function setData($key, $val, $expare = 60)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        self::$redisHander->set($key, $val);
        $time = time() + $expare;
        $expireTime = mktime(date("H", $time), date("i", $time), date("i", $time), date("m", $time), date("d", $time), date("Y", $time));
        return self::$redisHander->expireAt($key, $expireTime);
    }

    /**
     * 设置一个值
     * @param $key
     * @param $val
     * @return mixed
     */
    static public function getData($key)
    {
        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->get($key);
    }


    /**
     * 删除一个key
     * @param $key
     * @return mixed
     */
    static public function delKey($key)
    {

        if (empty(self::$redisHander)) {
            self::connect();
        }
        return self::$redisHander->del($key);
    }


}

//useage:系统自定义队列使用
// \app\queue\Queue::push("Addusermoneytask", json_encode(['path' => "\app\\task\controller\Addusermoneytask#task", "key" => getTaskKey($solt), 'param' => array()]));
//\app\queue\Queue::pop("Addusermoneytask");

//用户级别就使用一个队列 依次执行就可以了
//\app\queue\Queue::userPush( json_encode(['path' => "\app\\task\controller\Addusermoneytask#task", "key" => getTaskKey(), 'param' => array()]));
//\app\queue\Queue::userPop();